package com.wust.footprint;

import java.util.Date;

import android.app.Activity;
import android.app.AlertDialog;
import android.app.Dialog;
import android.app.AlertDialog.Builder;
import android.content.DialogInterface;
import android.content.Intent;
import android.content.SharedPreferences;
import android.graphics.Color;
import android.os.Bundle;
import android.view.View;
import android.view.ViewGroup;
import android.view.View.OnClickListener;
import android.view.ViewGroup.LayoutParams;
import android.widget.AbsListView;
import android.widget.BaseAdapter;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageButton;
import android.widget.TextView;

import com.wust.R;
import com.wust.R.drawable;

import com.wust.diary.Diary;
import com.wust.diary.DiaryDao;
import com.wust.user.User;
import com.wust.util.SqltimeToTime;

public class diaryAdd extends Activity {
	private ImageButton diary_mood=null;
	private Button save=null;
	private Button cancel=null;
	private EditText diarytitle=null;
	private EditText diarycontent=null;
	private TextView diarydate=null;
	private TextView dairy_add_date=null;
	private DiaryDao diaryDao;
	private Diary diary;
	private String user_name;
	private SqltimeToTime dateTransfer;
	private int[] imgIds = {R.drawable.mood_biaoyang, 
			R.drawable.mood_bishi,R.drawable.mood_kaixin,R.drawable.mood_keai,
			R.drawable.mood_sese,R.drawable.mood_shangxin,R.drawable.mood_shuaku,R.drawable.mood_yihuo};
	private final int ICON_LIST_DIALOG = 1;
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		super.onCreate(savedInstanceState);
		setContentView(R.layout.diary_add);
		diary_mood=(ImageButton)findViewById(R.id.diary_mood);
		save=(Button)findViewById(R.id.savediary);
		cancel=(Button)findViewById(R.id.canceldiary);
		diarytitle=(EditText)findViewById(R.id.title2);
		diarycontent=(EditText)findViewById(R.id.diary_content);
		dairy_add_date=(TextView)findViewById(R.id.dairy_add_date);
		SqltimeToTime transfertime=new SqltimeToTime();
		String date=transfertime.dateToString(new Date(System.currentTimeMillis()), "yyyy-MM-dd");
		dairy_add_date.setText(date);
		diaryDao=new DiaryDao(this);
		dateTransfer=new SqltimeToTime();
		diary_mood.setOnClickListener(new ImageClickListener());
		SharedPreferences preferences = getSharedPreferences("userinfo",0);
		user_name=preferences.getString("user_name", "");
		save.setOnClickListener(new DirayListener());
		cancel.setOnClickListener(new CacelListener());
	}
	
	class CacelListener implements OnClickListener
	{

		@Override
		public void onClick(View arg0) {
			// TODO Auto-generated method stub
			Intent intent=new Intent();
			intent.setClass(diaryAdd.this, diaryList.class);
			
			diaryAdd.this.finish();
		}
		
	}
	private class ImageClickListener implements OnClickListener{
		public void onClick(View v) {
			// TODO Auto-generated method stub
			 //Dialog dialog = new moodDialog(diaryAdd.this,R.style.moodDialog);                
			 //dialog.show();
			showDialog(ICON_LIST_DIALOG);
		}
	}
	private class DirayListener implements OnClickListener
	{

		public void onClick(View v) {
			// TODO Auto-generated method stub
			saveDiary();
		}
		
	}
	private void saveDiary()
	{
		diary=new Diary();
		Date date=new Date(System.currentTimeMillis());
		String curdate=dateTransfer.dateToString(date,"yyyy-MM-dd hh:mm:ss");
		if(diarytitle.getText().equals(""))
		{
			diarytitle.setText("新建日志");
		}
		else
		{
			diary.setDiary_title(diarytitle.getText().toString());
			diary.setDiary_content(diarycontent.getText().toString());
			diary.setDiary_date(curdate);
		}
	
		diary.setUser_name(user_name);
		diaryDao.insertDiary(diary);
		Intent intent=new Intent();
		intent.setClass(this, diaryList.class);
		this.setResult(this.RESULT_OK, intent);
		this.finish();
		//this.startActivity(intent);
	}
	@Override
    protected Dialog onCreateDialog(int id) {
    	Dialog dialog = null;
    	switch(id) {
    		case ICON_LIST_DIALOG:
    			Builder builder = new AlertDialog.Builder(this);
    			builder.setIcon(R.drawable.diary_mood);
    			builder.setTitle("心情");
    			BaseAdapter adapter = new ListItemAdapter();
    			DialogInterface.OnClickListener listener = 
    				new DialogInterface.OnClickListener() {
						public void onClick(DialogInterface dialogInterface, int which) {
							diary_mood.setImageResource(imgIds[which]);
						}
					};
				builder.setAdapter(adapter, listener);
    			dialog = builder.create();
    			break;
    	}
    	return dialog;
    }
	
	class ListItemAdapter extends BaseAdapter {
		public int getCount() {
			return imgIds.length;
		}
		public Object getItem(int position) {
			return null;
		}
		public long getItemId(int position) {
			return 0;
		}
		public View getView(int position, 
				View contentView, ViewGroup parent) {
			TextView textView = 
				new TextView(diaryAdd.this);
			//获得array.xml中的数组资源getStringArray返回的是一个String数组
			String text = getResources().getStringArray(R.array.hobby)[position];
			textView.setText(text);
			//设置字体大小
			textView.setTextSize(24);
			AbsListView.LayoutParams layoutParams = new AbsListView.LayoutParams(
					LayoutParams.FILL_PARENT,LayoutParams.WRAP_CONTENT);
			textView.setLayoutParams(layoutParams);
			//设置水平方向上居中
			textView.setGravity(android.view.Gravity.CENTER_VERTICAL);
			textView.setMinHeight(65);
			//设置文字颜色
			textView.setTextColor(Color.BLACK);  
			//设置图标在文字的左边
			textView.setCompoundDrawablesWithIntrinsicBounds(imgIds[position], 0, 0, 0);
			//设置textView的左上右下的padding大小
			textView.setPadding(15, 0, 15, 0);
			//设置文字和图标之间的padding大小
			textView.setCompoundDrawablePadding(15);
			return textView;
		}
    }
}
